/**
 */
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('AgreementListCtrl', ['$scope','InsuranceService','CommunityService','$http','$state','$stateParams','AgreementService','BaseURL','Modal',
        function ($scope,InsuranceService,CommunityService,$http,$state,$routeParams,agreementService,baseUrl,Modal) {

		$scope.communityService = CommunityService;
		 CommunityService.getBaseCode('{codeType:\'agreementType\'}');
		 CommunityService.getBaseCode('{codeType:\'areas\'}');
			InsuranceService.initMedicalOrgInfoList().success(function(response){
	            $scope.medicalOrgInfoList = response.medicalOrgList;
			});
            $scope.oldAgreementDetail={};
            /**
             * @description     url参数请求对象
             */
//            $scope.UrlParams = $routeParams;
            $scope.currentPage = 1;
            //表头
            $scope.columns = [
                             {label:'协议编号',map:'agreementNo',width:'20%',cellTemplate:'<a  ng:bind="data.agreementNo" '+
                                'ui-sref="agreement.detail({providerid:data.id})"></a>'},
  							{label:'机构名称',map:'healthcareProviderName',width:'20%'},
                            {label:'机构类别',map:'organizationType',width:'15%'},
  							{label:'协议类型',map:'agreementTypeName',width:'15%'},
  							{label:'协议生效日期',map:'dateAgreementEff',width:'15%'},
  							{label:'协议终止日期',map:'dateAgreementEnd',width:'15%'}
  						];
            var start = 0,
                limit = 20;
          //查询
    		$scope.onSubmit = function(){
    			query();
    		}

    		 $scope.agreementListParam = {
    	                page:"1",
    	                pageSize:"20"
    	            };
            var query =	function(page){
            	if(!page){
            		page=1;
            	}
            	 $scope.currentPage = page;
                $scope.agreementListParam.page=0;
                if(angular.isUndefined($scope.currentPage) == false && $scope.currentPage > 1){
                    $scope.agreementListParam.page = ($scope.currentPage - 1) * 20;
                }
              var params = $scope.agreementListParam;
              agreementService.query(params).success(function(data){
                      $scope.tableData = data.root;
                      $scope.totalCount = data.total;
              });
              
    		}


    		//分页监听
    		$scope.$on('pageTool.skipPage',function(e,v){
               
    			query(v);
    		});
            /**
             * 协议--重置
             * @param params
             */
            $scope.onReset=function()
            {
                $scope.agreementListParam={};
       		 $scope.agreementListParam = {
 	                page:"1",
 	                pageSize:"20"
 	            };
            }
            $scope.checkRows = [];

            function findCheck(d) {
                for(var i=0;i< $scope.checkRows .length;i++) {
                    var checkedData =  $scope.checkRows [i];
                    if(checkedData.itemId == d.itemId) {
                        return i;
                    }
                }
                return -1;
            }

            $scope.$on('fwatable.checked', function(e,d){
                if(findCheck(d) == -1) {
                    $scope.checkRows .push(d);
                }
            });

            $scope.$on('fwatable.unchecked', function(e,d){
                var index = findCheck(d);
                if(index != -1) {
                    $scope.checkRows.splice(index,1);
                }
            });
            /**
             * 协议列表--删除
             * @param params
             */
            $scope.deleteAgreementList = function(){
                var	checkRows =  $scope.checkRows;
                if (checkRows.length == 0) {
                    Modal.alert("请勾选删除项！");
                    return;
                }else
                {
                    var ids = '';
                    angular.forEach(checkRows,function(totalCheck,i){
                        ids+="'"+totalCheck.id+"',";
                    });
                    var  params= {
                        id : ids
                    }
                    Modal.confirm('确认删除吗？',function() {
                        $http.post(baseUrl + 'agreement/delete.do', params).success(function (data) {
                            if (data.state == '0') {
                                Modal.alert(data.msg);
                            } else {
                                Modal.alert("删除失败！");
                            }
                            $scope.onSubmit();
                        })
                    })
                }
            };
            /**
             * 协议列表--新增
             * @param params
             */
    		$scope.addAgreement = function () {
    			$state.go("agreement.addAgreement");
    		};

            /**
             * 列表复选框--全选控制
             * @type {{}}
             */
            $scope.getAgreementIdList = function(domName,isArray,prefix){
                $scope.agreementIdList = Utils.getCheckBoxValue(domName,isArray,prefix);
            };
            $scope.onSubmit();
        }
    ]);
})();